package se.miun.dt156g.antonsskafferibestallning;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class ApetizerActivity extends Activity implements OnClickListener {

	private ArrayList<String> drinkNamesArrayList;
	private HashMap<String, Integer> noOfDrinksHashMap;
	private int tableNo;

	private ProgressDialog pDialog;

	private static final String READ_URL = "http://pellos.no-ip.org/android/apetizer.php";
	private static final String TAG_POSTS = "posts";
	private static final String TAG_APETIZER = "apetizer";

	private JSONArray apetizerMenu = null;

	private ArrayList<String> apetizerNamesArrayList;
	HashMap<String, Integer> noOfApetizersHashMap = new HashMap<String, Integer>();

	private ListView apetizersListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_apetizer);

		Bundle extras = getIntent().getExtras();

		drinkNamesArrayList = extras.getStringArrayList("arraylist");
		noOfDrinksHashMap = (HashMap<String, Integer>) extras.get("hashmap");
		tableNo = extras.getInt("tableno");

		apetizersListView = (ListView) findViewById(R.id.apetizerListView);

		Button nextButton = (Button) findViewById(R.id.apetizersNextButton);
		nextButton.setOnClickListener(this);

		new LoadComments().execute();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.apetizer, menu);
		return true;
	}

	public void updateJSONdata() {

		apetizerNamesArrayList = new ArrayList<String>();

		JSONParser jParser = new JSONParser();

		JSONObject json = jParser.getJSONFromUrl(READ_URL);

		try {

			apetizerMenu = json.getJSONArray(TAG_POSTS);

			for (int i = 0; i < apetizerMenu.length(); i++) {
				JSONObject c = apetizerMenu.getJSONObject(i);

				apetizerNamesArrayList.add(c.getString(TAG_APETIZER));
				noOfApetizersHashMap.put(c.getString(TAG_APETIZER), 0);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void updateList() {

		MultiSourceAdapter adapter = new MultiSourceAdapter(
				getApplicationContext(), apetizerNamesArrayList,
				noOfApetizersHashMap);

		apetizersListView.setAdapter(adapter);
		
		apetizersListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				noOfApetizersHashMap.put(apetizerNamesArrayList.get(position),
						noOfApetizersHashMap.get(apetizerNamesArrayList.get(position)) - 1);
				updateList();
				return true;
				
			}
			
		});

		apetizersListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				noOfApetizersHashMap.put(apetizerNamesArrayList.get(position),
						noOfApetizersHashMap.get(apetizerNamesArrayList
								.get(position)) + 1);
				updateList();
			}
		});
	}

	public class LoadComments extends AsyncTask<Void, Void, Boolean> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(ApetizerActivity.this);
			pDialog.setMessage("Laddar meny...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		@Override
		protected Boolean doInBackground(Void... arg0) {
			updateJSONdata();
			return null;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			pDialog.dismiss();
			updateList();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.apetizersNextButton:
			Intent startMainCourseActivityIntent = new Intent(this,
					MainCourseActivity.class);
			startMainCourseActivityIntent.putExtra("drinksArraylist",
					drinkNamesArrayList);
			startMainCourseActivityIntent.putExtra("drinksHashMap",
					noOfDrinksHashMap);
			startMainCourseActivityIntent.putExtra("apetizersArraylist",
					apetizerNamesArrayList);
			startMainCourseActivityIntent.putExtra("apetizersHashMap",
					noOfApetizersHashMap);
			startMainCourseActivityIntent.putExtra("tableno", tableNo);
			startActivity(startMainCourseActivityIntent);
			finish();
			break;
		}
	}
}